package com.chzker.action;

import javax.annotation.Resource;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.chzker.model.Users;
import com.chzker.service.UsersManager;
import com.chzker.util.MD5;
import com.opensymphony.xwork2.ActionContext;
  
@Component("users")
@Scope("prototype")   
public class UsersAction extends BaseAction {  
  
//===============================私有变量====================================//
    private UsersManager usersManager;
    private String userName;
    private String password;
    private String password2;
    private int state;
    private int type;
    
    
//==================================登录=====================================//
    public String login() {  
    	Users user = new Users();
    	if(userName!=null&&password!=null){
    		user.setUsername(userName);
    		user.setPassword(MD5.MD5Encode(password));
    		//System.out.println(MD5.MD5Encode(password));
    		String result=usersManager.login(user);
    		if(result.equals("loginsuccess")){
    			//Map session = ActionContext.getContext().getSession();
    			//session.put("User", userName);
    			//System.out.println("+++++++here");
    			ActionContext.getContext().getSession().put("user",userName);  
    	        //ActionContext.getContext().getSession().put("password",password);  
    			//Cookie cookie = new Cookie("user",userName);
    			//res.addCookie(cookie);
    		}
    		return result;  
    	}else{
    		return "error";
    	}
    }
    
//===============================注销登陆用户==================================//
    public String userExit(){
    	System.out.println("======当前账户====="+ActionContext.getContext().getSession().get("user"));
    	if(ActionContext.getContext().getSession().get("user")!=null){
    		ActionContext.getContext().getSession().remove("user");
    		System.out.println("====注销成功===="+ActionContext.getContext().getSession().get("user"));
    	}
    	return "exit";
    }

//=================================添加用户====================================//    
    public String add() {
    	Users user = new Users();
    	if(userName!=null&&password!=null){
    		user.setUsername(userName);
    		user.setPassword(MD5.MD5Encode(password));
    		user.setType(type);
    		String result=usersManager.add(user);
    		return result;
    	}else{
    		return "error";
    	}
    }
    
  //=================================修改用户密码====================================//    
    public String update() {
    	Users user = new Users();
    	if(userName!=null&&password!=null){
    		user.setUsername(userName);
    		user.setPassword(MD5.MD5Encode(password));
    		user.setType(type);
    		String result=usersManager.update(user);
    		return result;
    	}else{
    		return "error";
    	}
    }
    
//===============================默认执行方法==================================//    
    public String execute() throws Exception {  
        // TODO Auto-generated method stub  
    /*	 Users user = new Users();
    	 user.setUsername("cuihaiyang");  
    	 user.setPassword("abcd");      		  
    	 usersManager.add(user);*/
        return "error";  
    }  
    
//*****************************Getter and Setter*******************************//
    public UsersManager getUsersManager() {
		return usersManager;
	}
    @Resource
	public void setUsersManager(UsersManager usersManager) {
		this.usersManager = usersManager;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
    
} 
